import click
import time
from dp.engine import Engine


@click.command()
@click.option("--sensitivity", default=1.0, help="DP sensitivity")
@click.option("--epsilon", default=0.1, help="DP epsilon")
@click.option("--input", default="io/query.sql", help="DP epsilon")
@click.option("--output", default="io/output.txt", help="DP epsilon")
@click.option("--database", default="test", help="PostgreSQL database")
@click.option("--user", default="quincy", help="PostgreSQL username")
@click.option("--password", default="123456", help="PostgreSQL password")
@click.option("--host", default="localhost", help="PostgreSQL host")
@click.option("--port", default=5432, help="PostgreSQL port")
@click.option(
    "--model",
    default="glove-wiki-gigaword-50",
    help="word2vec model, include glove-wiki-gigaword-50,  glove-twitter-25, et al",
)
@click.option(
    "--dp",
    default="rdp",
    help="repeatable dp(rdp), personalized dp(pdp), traditional dp(tdp) ",
)
def run(
    sensitivity, epsilon, input, output, database, user, password, host, port, model, dp
):
    engine = Engine(
        database=database,
        user=user,
        password=password,
        host=host,
        port=port,
        model=model,
    )
    predictions = []
    with open(input, "r") as f:
        for sql in f.readlines():
            start = time.time()
            print("query is ", sql)
            p = engine.query(sql, sensitivity=sensitivity,
                             epsilon=epsilon, dp=dp)
            # time in seconds
            predictions.append((list(p), time.time() - start))

    with open(output, "w") as f:
        for line in predictions:
            f.write("{},{}\n".format(line[0][0], line[1]))

    print("Application is closed successfully.")


if __name__ == "__main__":
    run()
